<?php
namespace Home\Controller;
use Think\Controller;

class FuncController extends Controller{


	protected function parconfig($str){
		$array=array();
		$strarray=explode("\n",str_replace("\r","",$str));
		foreach ($strarray as $one){
			$ra=$rw=explode("||",$one);
			unset($ra[0]);
			$array[$rw[0]]=$ra;
		}
		return $array;
	}


	protected function http( $url, $ua = "" ){
        $opts = array(
            "http" => array(
				'timeout'=>3,
                "header" => "USER-AGENT: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36,Referer:http://www.taobao.com/",
            )
        );
        $context = stream_context_create( $opts );
        $html = @file_get_contents( $url, FALSE, $context );
        if($html === FALSE){
            $html = @file_get_contents( $url, FALSE, $context );
            if($html === FALSE){
                $html = @file_get_contents( $url, FALSE, $context );
            }
        }
        for($i = 0;$i <3;++$i){
            if(!($html === FALSE)){
                break;
            }
            $html = @file_get_contents( $url, FALSE, $context );
        }
		if($html === FALSE){
			$ch = curl_init(); 
			curl_setopt($ch, CURLOPT_URL, $url); 
			curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:58.23.4.141', 'CLIENT-IP:58.23.4.141'));//IP 
			curl_setopt($ch, CURLOPT_REFERER, "http://s.m.taobao.com/h5");   //��· 
			curl_setopt($ch, CURLOPT_HEADER, 1);
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); //timeout on connect
			curl_setopt($ch, CURLOPT_TIMEOUT, 5); //timeout on response
			$html = curl_exec($ch); 
			curl_close($ch);
		}
        return $html;
    }


   

	protected function str_mid_replace($string) {
		if (! $string || !isset($string[1])) return $string;
		$len = strlen($string);
		$starNum = floor($len / 2); 
		$noStarNum = $len - $starNum;
		$leftNum = ceil($noStarNum / 2); 
		$starPos = $leftNum;
		for($i=0; $i<$starNum; $i++) $string[$starPos+$i] = '*';

		return $string;
	}

 
    protected function ajaxReturn($status=1, $msg='', $data='', $dialog='') {
        parent::ajaxReturn(array(
            'status' => $status,
            'msg' => $msg,
            'data' => $data,
            'dialog' => $dialog,
        ));
    }

	protected function jsonReturn($data,$type='JSON'){
    	header('Content-Type:application/json; charset=utf-8');
    	exit(json_encode($data));
    }
}